package makovo.ajax;

import java.io.IOException;
import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;

import makovo.data.Items;

import org.apache.http.client.ClientProtocolException;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

public class GeoInfo extends ActionSupport implements ServletRequestAware, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HttpServletRequest mRequest;
	private String mCities;
	private String mTowns;

	public String getCities() {
		return mCities;
	}

	public String getTowns() {
		return mTowns;
	}

	public String showCities() {
		try {
			String stateId = this.mRequest.getParameter("state");

			String cities = Items.GeoInfo.getCities(Integer.valueOf(stateId));
			this.mCities = cities;

		} catch (ClientProtocolException e) {
			e.printStackTrace();
			return "ERROR";
		} catch (IOException e) {
			e.printStackTrace();
			return "ERROR";
		}

		return ActionSupport.SUCCESS;
	}

	public String showTowns() {
		try {
			String stateId = this.mRequest.getParameter("state");
			String cityId = this.mRequest.getParameter("city");

			String towns = Items.GeoInfo.getTowns(Integer.valueOf(stateId), Integer.valueOf(cityId));
			this.mTowns = towns;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			return "ERROR";
		} catch (IOException e) {
			e.printStackTrace();
			return "ERROR";
		}
		
		return ActionSupport.SUCCESS;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.mRequest = request;
	}
}
